package com.hulu.yec.sys.yecclass.service;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hulu.yec.common.Constant;
import com.hulu.yec.sys.yecclass.entity.ClassEntity;

public class ClassAddDao {

	/**
	 * 用户名唯一性判断
	 * 
	 * @param sql
	 * @param name
	 * @param request
	 * @param response
	 * @throws SQLException
	 * @throws IOException
	 */
	public static void ListId(String sql, String name,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, IOException {

		response.setCharacterEncoding(Constant.ENCODING_UTF8);
		if (name != "") {
			List<ClassEntity> listUser = new ArrayList<>();
			Connection connection = null;
			PreparedStatement preparedStatement = null;
			ResultSet resultSet = null;
			try {
				Class.forName("com.mysql.jdbc.Driver");
				connection = DriverManager.getConnection(
						"jdbc:mysql://localhost:3306/exam", "root", "root");

				preparedStatement = connection.prepareStatement(sql);

				resultSet = preparedStatement.executeQuery();

				while (resultSet.next()) {
					ClassEntity listEntity = new ClassEntity();

					listEntity.setId(resultSet.getInt("id"));
					listUser.add(listEntity);
				}

				if (listUser.size() != 0) {
					String print = "　　用户已存在!";
					response.getWriter().print(print);

				} else if (listUser.size() == 0) {
					String print = "　通过";
					response.getWriter().print(print);
				}

			} catch (ClassNotFoundException | SQLException e) {

				throw new RuntimeException("失败~");
			} finally {
				// 4、资源关闭

				if (resultSet != null) {
					resultSet.close();
				}
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}

			}

		} else {
			String print = "　　用户名不能为空!";
			response.getWriter().print(print);
		}
	}

	/**
	 * 状态的修改
	 * 
	 * @param sql
	 * @return
	 * @throws SQLException
	 */
	public static int states(String sql) throws SQLException {

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/exam", "root", "root");

			preparedStatement = connection.prepareStatement(sql);
			return preparedStatement.executeUpdate(sql);

		} catch (ClassNotFoundException | SQLException e) {
			throw new RuntimeException("简历查询失败~");
		} finally {
			// 4、资源关闭

			if (resultSet != null) {
				resultSet.close();
			}
			if (preparedStatement != null) {
				preparedStatement.close();
			}
			if (connection != null) {
				connection.close();
			}

		}

	}

}
